package com.Sdebruijn.curriculumvitae.xmpp;

import java.util.ArrayList;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import com.Sdebruijn.secrets.chat.Secret;

import android.util.Log;

public class XmppManager implements Secret {

	private XMPPConnection connection;
	public ArrayList<String> messages = new ArrayList<String>();

	private OnNewMessageListener mOnNewMessageListener;

	public XmppManager(OnNewMessageListener listener) {
		mOnNewMessageListener = listener;
		// Create a connection
		ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222,
				"gmail.com");
		connection = new XMPPConnection(connConfig);

		try {
			connection.connect();
			Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
		} catch (XMPPException ex) {
			Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
			Log.e("XMPPClient", ex.toString());
			setConnection(null);
		}
		try {
			connection.login(GMAIL_ACCOUNT, GMAIL_PASS);
			Log.i("XMPPClient", "Logged in as " + connection.getUser());

			// Set the status to available
			Presence presence = new Presence(Presence.Type.available);
			connection.sendPacket(presence);
			setConnection(connection);
		} catch (XMPPException ex) {
			Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + GMAIL_ACCOUNT);
			Log.e("XMPPClient", ex.toString());
			setConnection(null);
		}
	}

	/**
	 * Called by Settings dialog when a connection is established with the XMPP
	 * server
	 * 
	 * @param connection
	 */
	public void setConnection(XMPPConnection connection) {
		this.connection = connection;
		if (connection != null) {
			// Add a packet listener to get messages sent to us
			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener() {
				public void processPacket(Packet packet) {
					Message message = (Message) packet;
					if (message.getBody() != null) {
						String fromName = StringUtils.parseBareAddress(message.getFrom());
						Log.i("XMPPClient", "Got text [" + message.getBody() + "] from ["
								+ fromName + "]");
						messages.add(fromName + ":");
						messages.add(message.getBody());
						// Add the incoming message to the list view
						mOnNewMessageListener.OnNewMessageReceived(message);
					}
				}
			}, filter);
		}
	}

	public Message buildMessage(String to, String text) {
		return buildMessage(to, text, Message.Type.chat);
	}

	public Message buildMessage(String to, String text, Message.Type type) {
		Message message = new Message();
		message.setTo(to);
		message.setBody(text);
		message.setType(type);
		return message;
	}

	public void sendMessage(Message message) {
		message.setType(Message.Type.chat);
		connection.sendPacket(message);

		messages.add(connection.getUser() + ":");
		messages.add(message.getBody());

		mOnNewMessageListener.OnNewMessageSend(message);

		// TODO update messages
		// setListAdapter();
	}

}
